package com.mezz.bones.im.logic.controller;

import com.mezz.bones.framework.base.domain.response.Result;
import com.mezz.bones.framework.cache.redis.service.LockService;
import com.mezz.bones.im.common.service.IAuthService;
import com.mezz.bones.im.logic.domain.request.ApiUserDeleteRequest;
import com.mezz.bones.im.logic.domain.request.ApiUserImportRequest;
import com.mezz.bones.im.logic.domain.request.ApiUserUpdateRequest;
import com.mezz.bones.im.logic.service.IChatUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api("IM用户管理")
@RequestMapping("/im/user")
@RestController
public class ChatUserController {

    @Resource
    private IChatUserService chatUserService;

    @Resource
    private LockService lockService;

    @Resource
    private IAuthService authService;


//    @PostMapping("/sign")
//    @ApiOperation("用户签名")
//    public Result<StringResponse> userSign(@Valid @RequestBody UserSignRequest request) {
//        return Result.OK(StringResponse.builder().str(authService.generateToken(request)).build());
//    }

    @PostMapping("/import")
    @ApiOperation("导入IM用户信息")
    public Result<?> importUser(@Valid @RequestBody ApiUserImportRequest request) {

        lockService.execWithLock("im:user:import:" + request.getUserId(), (Void) -> {
            chatUserService.importUser(request);
        });

        return Result.OK();
    }

    @PostMapping("/update")
    @ApiOperation("更新IM用户信息")
    public Result<?> updateUser(@Valid @RequestBody ApiUserUpdateRequest request) {

        lockService.execWithLock("im:user:update:" + request.getUserId(), (Void) -> {
            chatUserService.updateUser(request);
        });

        return Result.OK();
    }


    @PostMapping("/delete")
    @ApiOperation("删除IM用户信息")
    public Result<?> deleteUser(@Valid @RequestBody ApiUserDeleteRequest request) {

        chatUserService.deleteUser(request);

        return Result.OK();
    }

}
